package com.example.productdataserviceprovider.service.city;

import com.example.productdataserviceprovider.service.AbstractService;
import com.example.productdataserviceprovider.service.search.model.SearchResultModel;
import com.example.productdataserviceprovider.service.utils.HttpUtils;
import org.springframework.util.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Deprecated
public class CityServiceImpl extends AbstractService implements  CityService {
    public final Pattern pattern= Pattern.compile("\"title\":\"(.*?)\",\"url\":\"(.*?)\"");
    @Override
    public Map<String, String> getCitys(String file) {
        Map<String,String> citys=new HashMap<>();
        BufferedReader reader=null;
        logger.info("start....");
        try {
            reader=new BufferedReader(new InputStreamReader(new FileInputStream(new File(file)),"GBK"));
            String line=null;
            while((line=reader.readLine())!=null){
                int index=line.indexOf("：");
                if(index!=-1){
                    int end=line.indexOf("。");
                    if(end==-1) {
                        line = line.substring(index + 1);
                    }else{
                        line=line.substring(index+1,end);
                    }
                    String[] datas=line.split("、");
//                    logger.info("line={}"+line);
                    for(String data:datas){
                        int kuohao=data.indexOf("（");
                        if(kuohao!=-1){
                            data=data.substring(0,kuohao);
                        }
                        data=data.trim();
                        citys.put(data,data);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(reader!=null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        logger.info("end...."+citys.size());
        return citys;
    }
    public Map<String, String> getProvinces(String file) {
        Map<String,String> citys=new HashMap<>();
        BufferedReader reader=null;
        logger.info("start....");
        try {
            reader=new BufferedReader(new InputStreamReader(new FileInputStream(new File(file)),"GBK"));
            String line=null;
            while((line=reader.readLine())!=null){
                line=line.replace("省","").replace("维吾尔自治区","").replace("壮族自治区","")
                .replace("回族自治区","").replace("特别行政区","").replace("自治区","")
                .replace("市","");
                citys.put(line,line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(reader!=null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        logger.info("end...."+citys.size());
        return citys;
    }
    @Override
    public void writerToFile(Map<String,SearchResultModel> datas, File resultFile) {
        BufferedWriter writer=null;
        try {
            writer=new BufferedWriter(new FileWriter(resultFile));
            int index=0;
            for(Map.Entry<String, SearchResultModel> vo:datas.entrySet()){
//            for(SearchResultModel data:datas){
                SearchResultModel data=vo.getValue();
                writer.write(data.getCity() +","+data.getTitle()+","+data.getUrl());
                writer.newLine();

                index++;
                if(index%100==0){
                    writer.flush();
                }
            }
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public SearchResultModel parseSearchResult(String result,String book,String city){
        List<SearchResultModel> results=new ArrayList<>();
        Matcher matcher=pattern.matcher(result);
        SearchResultModel model=null;
        while(matcher.find()){
            String title=matcher.group(1);
            if(title.indexOf(book)!=-1&&title.indexOf(city)!=-1) {
                model=new SearchResultModel();
                model.setTitle(matcher.group(1));
                model.setUrl(matcher.group(2));
                return model;
            }else{
                continue;
            }
        }
        return model;
    }
    public void writerCitysToFile(Map<String,String> citys,String file){
        BufferedWriter writer=null;
        try {
            writer=new BufferedWriter(new FileWriter(new File(file)));
            for(Map.Entry<String,String> city:citys.entrySet()){
                writer.write(city.getKey());
                writer.newLine();
            }
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(writer!=null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Override
    public Map<String,String> getCitysFromUrl(String url) {
        Map<String,String> citys=new HashMap<>();
        try {
            String result= HttpUtils.doGet(url);
            Pattern pattern=Pattern.compile("省(.*?)市");
            Matcher matcher=pattern.matcher(result);
            while(matcher.find()){
                String city=matcher.group(1);
                citys.put(city,"");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("totalSize="+citys.size());
        return citys;
    }
    public Map<String,String> getCitysFromFiles(String cityFile) {
        Map<String,String> citys=new HashMap<>();
        BufferedReader reader=null;
        try {
            reader=new BufferedReader(new InputStreamReader(new FileInputStream(cityFile),"GBK"));
            String line=null;
            while((line=reader.readLine())!=null){
                String[] cityDatas=line.split("、");
                for(String city:cityDatas){
                    citys.put(city,"");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("totalSize="+citys.size());
        return citys;
    }
    public void analysisCitys(Map<String,String> citys,String keyWords,String resultFile){
        SearchResultModel model=new SearchResultModel();
        List<SearchResultModel> resultList=new ArrayList<>();
        int page=1;
        int total=citys.size();
        int current=0;
        BufferedWriter writer=null;
        resultFile=resultFile.replaceAll("\\{\\}",keyWords);
        try {
            writer=new BufferedWriter(new FileWriter(new File(resultFile)));
            for(Map.Entry<String,String> city:citys.entrySet())
            {
                String cityName=city.getKey();
                logger.info("cityName="+cityName+",keyWord="+keyWords);
                current++;
                logger.info("当前值:"+current+",total="+total);
                //if(current<1148)continue;
                model.setKey(keyWords);
                model.setPage(0);
                model.setUrl("https://www.baidu.com/s?wd="+cityName+"%20"+model.getKey()+"&pn="+model.getPage());
                logger.info("url="+model.getUrl());
                String result="";
                try {
                    result=HttpUtils.doGet(model.getUrl()).replaceAll("(\r\n|\r|\n|\n\r)","");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if(!StringUtils.hasText(result)){
                    logger.info("搜索结果为空");
                    continue;
                }
                //解析URL
                SearchResultModel data= parseSearchResult(result,keyWords,cityName);
                if(data!=null){
                    data.setCity(cityName);
                    writer.write(data.getCity() +","+data.getTitle()+","+data.getUrl());
                    writer.newLine();
                    writer.flush();
                }

            }
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally
        {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



    @Override
    public void composeFiles(List<File> files, File dest, boolean isDelete) {
        BufferedWriter writer=null;
        try {
            writer=new BufferedWriter(new FileWriter(dest));
            for(File file:files) {
                BufferedReader reader=new BufferedReader(new FileReader(file));
                String line=null;
                while((line=reader.readLine())!=null){
                    writer.write(line);
                    writer.newLine();
                    writer.flush();
                }
                writer.flush();
                if(reader!=null)reader.close();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                for(File file:files){
                    if(isDelete){
                        if(file.exists())file.delete();
                    }
                }
            }
        }
    }

    public static void main2(String[] args) {
        CityService service=new CityServiceImpl();
        Map<String,String> citys=service.getCitys("D:/jd/city.txt");

//        String keyWords="城市书房";
//        String keyWords="城市书房、图书馆驿站、悦·书房、三味书咖、阅读小镇、码头书屋、书香酒店、宣阳驿站、城市公共阅读空间、新型公共阅读空间、新型阅读空间、特色阅读空间、24小时阅读空间、城市阅读点";
        String keyWords="悦·书房、三味书咖、阅读小镇、码头书屋、书香酒店、宣阳驿站、城市公共阅读空间、新型公共阅读空间、新型阅读空间、特色阅读空间、24小时阅读空间、城市阅读点";
        String[] keys=keyWords.split("、");
        SearchResultModel model=new SearchResultModel();
        List<SearchResultModel> resultList=new ArrayList<>();

        String resultFile="D:/jd/resultData-{}.txt";
        int page=2;

        for(String key:keys){
            model.setKey(key);
            for(int i=0;i<page;i++){
                logger.info("执行第"+(i+1)+"页...");
                model.setPage(i*10);
                model.setUrl("https://www.baidu.com/s?wd="+model.getKey()+"&pn="+model.getPage());
                logger.info("url="+model.getUrl());
                String result="";
                try {
                    result=HttpUtils.doGet(model.getUrl()).replaceAll("(\r\n|\r|\n|\n\r)","");
                } catch (Exception e) {
                    e.printStackTrace();
                }
//                logger.info("result="+result);
                if(!StringUtils.hasText(result)){
                    System.out.println("搜索结果为空");
                    return;
                }
                //解析URL
                List<SearchResultModel> datas=new ArrayList<>();// searchService.parseSearchResult(result);
                logger.info("data.size="+datas.size());
                resultList.addAll(datas);
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
            }
            logger.info("key:"+key+",size="+resultList.size());
            Map<String,SearchResultModel> resultMaps=new HashMap<>();//结果集
            for(SearchResultModel vo:resultList){
                String title=vo.getTitle();
                String url=vo.getUrl();
                int indexPlace=title.indexOf(key);
                if(indexPlace==-1)continue;
                List<String> words=new ArrayList<>();//searchService.segment(title);
                for(String word:words){
                    if(citys.containsKey(word)){
                        if(resultMaps.containsKey(word)){
                            break;
                        }
                        vo.setCity(word);
                        resultMaps.put(word,vo);
                    }
                }
            }
            logger.info("写入文件:");
            String destFile=resultFile.replaceAll("\\{\\}",key);
            service.writerToFile(resultMaps,new File(destFile));
        }
    }
    public static void main3(String[] args) {
        CityService service=new CityServiceImpl();
        Map<String,String> citys=service.getCitys("D:/jd/city.txt");
        String cityFile="D:/jd/cityFiles.txt";
        service.writerCitysToFile(citys,cityFile);
        String keyWords="城市书房";
//        String keyWords="城市书房、图书馆驿站、悦·书房、三味书咖、阅读小镇、码头书屋、书香酒店、宣阳驿站、城市公共阅读空间、新型公共阅读空间、新型阅读空间、特色阅读空间、24小时阅读空间、城市阅读点";
        String[] keys=keyWords.split("、");
        SearchResultModel model=new SearchResultModel();
        List<SearchResultModel> resultList=new ArrayList<>();

        String resultFile="D:/jd/result/resultData.txt";
        int page=1;
        Map<String,SearchResultModel> resultMaps=new HashMap<>();//结果集
        int total=citys.size();
        int current=0;
        BufferedWriter writer=null;
        try {
            writer=new BufferedWriter(new FileWriter(new File(resultFile)));
            for(Map.Entry<String,String> city:citys.entrySet()){
                String cityName=city.getKey();
                logger.info("cityName="+cityName);
                current++;
                // if(current==10)break;
                logger.info("当前值:"+current+",total="+total);
                for(String key:keys){
                    model.setKey(key);
                    model.setPage(0);
                    model.setUrl("https://www.baidu.com/s?wd="+cityName+"%20"+model.getKey()+"&pn="+model.getPage());
                    logger.info("url="+model.getUrl());
                    String result="";
                    long startTime=System.currentTimeMillis();
                    try {
                        result=HttpUtils.doGet(model.getUrl()).replaceAll("(\r\n|\r|\n|\n\r)","");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if(!StringUtils.hasText(result)){
                        logger.info("搜索结果为空");
                        continue;
                    }
                    //解析URL
                    SearchResultModel data= service.parseSearchResult(result,key,cityName);
                    if(data!=null){
                        data.setCity(cityName);
                        resultMaps.put(cityName,data);
                        writer.write(data.getCity() +","+data.getTitle()+","+data.getUrl());
                        writer.newLine();
                        writer.flush();
                        break;
                    }
                }
            }
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally
        {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        /*logger.info("写入文件");
        service.writerToFile(resultMaps,new File(resultFile));*/
    }

    public static void main(String[] args) {
        CityService service = new CityServiceImpl();
        Map<String, String> citys = service.getCitysFromFiles("D:/library/city/citys.txt");
//        Map<String, String> citys = service.getProvinces("D:/library/city/provinces.txt");
//        String keyWord="城市书房,领里图书馆,家庭图书分馆,家庭图书馆,网上借阅、社区投递,网上借阅社区投递,送书上门,手机借书,扫码借书,图书转借,文献转借,转借服务";
//        String keyWord="领里图书馆,家庭图书分馆,家庭图书馆,网上借阅、社区投递,网上借阅社区投递,送书上门,手机借书,扫码借书,图书转借,文献转借,转借服务";
//        String keyWord = "文化场馆免费开放实施办法,文化场馆免费开放工作的意见,文化场馆免费开放的通知,免费开放专项资金";//城市书房,邻里图书馆,家庭图书分馆,家庭图书馆
        String keyWord = "文化大院";//城市书房,邻里图书馆,家庭图书分馆,家庭图书馆
//        String keyWord = "文化场馆免费开放工作的意见,文化场馆免费开放的通知";//城市书房,邻里图书馆,家庭图书分馆,家庭图书馆
        String commonFile="resultProvincesDatasBanFa";
        String dirPath = "D:/library/city/result/";
        String resultFile = dirPath+commonFile+"-{}.txt";
        int capture = 5, totalSize = citys.size();
        int threadSize = totalSize % capture == 0 ? totalSize / capture : totalSize / capture + 1;
        Map<String, String>[] citysDatas = new HashMap[threadSize];
        for (int i = 0; i < threadSize; i++) {
            citysDatas[i] = new HashMap<>();
        }
        int index = 0;
        for (Map.Entry<String, String> city : citys.entrySet()) {
            citysDatas[index % threadSize].put(city.getKey(), "");
            index++;
        }
        CountDownLatch latch = new CountDownLatch(threadSize);

        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < threadSize; i++) {
            String resultThreadFile = dirPath + commonFile+"-{}-" + i + ".txt";
            logger.info("数量:" + i + ",Size=" + citysDatas[i].size());
            CityThread thread = new CityThread(service, citysDatas[i], keyWord, resultThreadFile, latch);
            executorService.submit(thread);
        }
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("开始合并文档===================");
        String[] keys = keyWord.split(",");
        for (String key : keys) {
            List<File> list = new ArrayList<>();
            File dir = new File(dirPath);
            File[] files = dir.listFiles();
            for (File file : files) {
                if (file.getName().indexOf("-" + key + "-") != -1) {
                    list.add(file);
                }
            }
            key = key.replaceAll("\\/", "");
            File dest = new File(dir.getPath() + File.separator + commonFile+"-" + key + "-汇总.txt");
            System.out.println("dest="+dest.getPath());
            service.composeFiles(list, dest, true);
        }
        executorService.shutdown();
        logger.info("END");
    }
}
